package com.shouer.doc.ui.billlist;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.shouer.doc.application.App;
import com.shouer.doc.R;
import com.shouer.doc.dialog.ShouerLoadingDialogUtil;
import com.shouer.doc.event.RemoteLoginEvent;
import com.shouer.doc.event.RongRemoteLoginEvent;
import com.shouer.doc.ui.billlist.adapter.BillListAdapter;
import com.shouer.doc.ui.billlist.presenter.BillListPresenterCompl;
import com.shouer.doc.ui.billlist.presenter.IBillListPresenter;
import com.shouer.doc.ui.billlist.view.IBillListView;
import com.shouer.doc.ui.login.model.UserModel;
import com.shouer.doc.ui.userinfo.model.BillModel;
import com.shouer.doc.widget.ShouerLayoutManager;
import com.shouer.doc.widget.ShouerListView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ZXL on 2016/12/20.
 */

public class BillListActivity extends AppCompatActivity implements IBillListView {

    private App app;
    private IBillListPresenter presenter;

    @BindView(R.id.bill_list)
    protected ShouerListView billListView;
    private BillListAdapter adapter;

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_billlist_layout);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        app = (App) getApplicationContext();
        presenter = new BillListPresenterCompl(this);
        initViewes();
        ShouerLoadingDialogUtil.getInstance().show(this);
        presenter.refresh();
    }

    private void initViewes() {
        ShouerLayoutManager layoutManager = new ShouerLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        billListView.setLayoutManager(layoutManager);
        billListView.setFlingScale(0.5);
        adapter = new BillListAdapter(this);
        billListView.setAdapter(adapter);
        billListView.setRefreshProgressStyle(ProgressStyle.SquareSpin);
        billListView.setLoadingMoreProgressStyle(ProgressStyle.SquareSpin);
        billListView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                if (!presenter.refresh()) {
                    billListView.refreshComplete();
                }
            }

            @Override
            public void onLoadMore() {
                if (!presenter.loadMore()) {
                    billListView.loadMoreComplete();
                }
            }
        });

    }

    @OnClick(value = {R.id.back_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        presenter.onDestroy();
    }


    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public UserModel getUser() {
        return app.getUserModel();
    }

    @Override
    public void refreshBillList(List<BillModel> billModels) {
        ShouerLoadingDialogUtil.getInstance().dismiss();
        billListView.loadMoreComplete();
        billListView.refreshComplete();
        adapter.refreshBillList(billModels);
    }

    @Override
    public void attachBillList(List<BillModel> billModels) {
        ShouerLoadingDialogUtil.getInstance().dismiss();
        billListView.loadMoreComplete();
        billListView.refreshComplete();
        adapter.attachToBillList(billModels);
    }

    @Override
    public void loadBillDataFailed(String failInfo) {
        ShouerLoadingDialogUtil.getInstance().dismiss();
        billListView.loadMoreComplete();
        billListView.refreshComplete();
        Toast.makeText(this, failInfo, Toast.LENGTH_SHORT).show();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(RemoteLoginEvent event)
    {
        finish();
    }

    /**
     * 账号异地登录 关闭Activity
     * */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(RongRemoteLoginEvent event) {
        finish();
    }

}
